package com.yunji.cabinet.context.convert;


import com.yunji.base.cache.BaseCache;
import com.yunji.cabinet.bo.CabinetBO;
import com.yunji.cabinet.bo.ContainerCellPriceBO;
import com.yunji.cabinet.bo.ContainerPriceBO;
import com.yunji.cabinet.cache.CabinetCache;
import com.yunji.cabinet.domain.ContainerCell;
import com.yunji.cabinet.domain.ContainerDO;
import com.yunji.cabinet.vo.CabinetCellVO;
import com.yunji.cabinet.vo.ContainerProductDetailVO;
import com.yunji.cabinet.vo.SpecificUpgradeCabinetVO;
import com.yunji.common.model.cache.ProductAgentCacheDTO;
import com.yunji.common.model.cache.ProductBelongCacheDTO;
import com.yunji.common.model.cache.ProductCacheDTO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.cache.ProductCache;

import java.util.ArrayList;
import java.util.List;

public class CabinetConvert {

    public static ContainerProductDetailVO priceCellBO2VO(ContainerCellPriceBO cellPriceBO) {
        ProductBelongCacheDTO productBelong = cellPriceBO.getProductBelong();
        ContainerProductDetailVO detailVO = new ContainerProductDetailVO();
        ProductAgentCacheDTO agent = productBelong.getProductAgentDTO();
        ProductCacheDTO productDTO = productBelong.getProductDTO();
        BeanUtils.clone(productDTO, detailVO);
        detailVO.setDiscountPrice(agent.getDiscountPrice());
        detailVO.setSale(200L);
        return detailVO;
    }

    public static List<CabinetCellVO> BOToVO(ContainerPriceBO bo) {
        List<CabinetCellVO> voList = new ArrayList<CabinetCellVO>();

        for (ContainerCellPriceBO cellPriceBO : bo.getCellPriceList()) {
            ContainerCell cell = cellPriceBO.getCell();
            CabinetCellVO vo = new CabinetCellVO();
            ProductAgentCacheDTO dto = cellPriceBO.getProductBelong().getProductAgentDTO();
            BeanUtils.clone(dto, vo);
            BeanUtils.clone(cell, vo);
            vo.setProductName(ProductCache.getProductName(cell.getProductId()));
            vo.setAlbumPics(ProductCache.getAlbumPics(cell.getProductId()));
            voList.add(vo);
        }

        return voList;
    }

    public static CabinetBO domainTOBO(ContainerDO cabinet) {
        if (cabinet == null) {
            return null;
        }
        return new CabinetBO(cabinet, CabinetCache.getCellList(cabinet.getContainerId()));
    }

    public static List<CabinetBO> domainTOBOList(List<ContainerDO> list) {
        List<CabinetBO> boList = new ArrayList<>();
        for (ContainerDO cabinet : list) {
            boList.add(domainTOBO(cabinet));
        }
        return boList;
    }

    public static List<SpecificUpgradeCabinetVO> domainTOSpecificList(List<ContainerDO> list) {
        List<SpecificUpgradeCabinetVO> voList = new ArrayList<>();
        for (ContainerDO cabinetDO : list) {
            SpecificUpgradeCabinetVO vo = new SpecificUpgradeCabinetVO();
            BeanUtils.clone(cabinetDO, vo);
            vo.setSiteName(BaseCache.getSiteName(cabinetDO.getSiteId()));
            vo.setAgentName(BaseCache.getAgentName(cabinetDO.getAgentId()));
            voList.add(vo);
        }
        return voList;
    }
}
